package com.fairytask.ui.petsUnit
{
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.ui.base.UnitUI;
	import com.fairytask.ui.item.HeadItem;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	                                                                                                                                       
	/**
	 * 角色及宠物列表 
	 * @author xujh
	 * 
	 */
	public class PetsListUI extends UnitUI
	{
		private static var _self		:PetsListUI;
		
		private static const MAXNUM	:int = 5;	//单页显示的头像数量
		
		public var bgMc		:MovieClip;		//背景
		public var photoVG		:ViewGroup;		//宠物头像列表
		
		private var _petsData	:Array	= [];	//头像列表
		
		private var _curSelect	:int	= -1;	//当前对象位置
		private var _curObj	:Point;			//当前选择对象objid
		private var _curUI		:int;			//当前打开的界面
		private var _profileUI	:PetEquipUI	= PetEquipUI.getInstance();
		
		private var _unbind	:Array = [];
		
		public static function getInstance():PetsListUI
		{
			if (_self == null)
			{
				_self=new PetsListUI;
			}
			return _self;
		}
		
		public function PetsListUI()
		{
			super();
			initVG();
			initBind();
			initEvent();
		}
		
		/**
		 * 初始化视图加锁 
		 * 
		 */
		private function initVG():void
		{
			for(var i:int=0;i<MAXNUM;i++)
			{
				var item	:HeadItem = new HeadItem();
				item.data = null;
				photoVG.getItemByIndex(i).addItem(item);
			}
		}
		
		/**
		 * 初始化绑定 
		 * 
		 */
		private function initBind():void
		{
			//绑定可用格
			/*bindManager.BindViewProp(model.scene.GetView(ViewDefine.VIEWPORT_PET),"Capacity",setPetCapacity);
			//宠物视图
			bindManager.BindView(model.scene,ViewDefine.VIEWPORT_PET,onAddPet,onRemovePet);*/
		}
		
		/**
		 * 初始化事件 
		 * 
		 */
		private function initEvent():void
		{
			photoVG.addItemsEvent(MouseEvent.CLICK,onClick);
		}
		
		/**
		 * 绑定宠物可用个数,解锁
		 * @param key
		 * @param val
		 * 
		 */
		public function setPetCapacity(key:String,val:int):void
		{
			if(val >= 5)
			{
				return;
			}
			for(var i:int=0;i<val;i++)
			{
				var item	:HeadItem = photoVG.getItemByIndex(i).getItem() as HeadItem;
				item.isLock = false;
			}
		}
		
		/**
		 * 单击头像 
		 * @param evt
		 * 
		 */
		private function onClick(evt:MouseEvent,pos:int):void
		{
			var item	:HeadItem = photoVG.getItemByIndex(pos).getItem() as HeadItem;
			//为开启
			if(item.isLock)
			{
				//
				return;
			}
			//空对象返回
			if(null == item.data)
			{
				return;
			}
			onSelect(pos);
		}
		
		/**
		 * 选中对象 
		 * 
		 */
		private function onSelect(pos:int):void
		{
			if(_curSelect == pos)
			{
				//融合或者吞噬的时候，相同也可以点击放入
				/*if(PetsUI.getInstance().checkUI(UnitUIDefine.PetsFuseUI))
				{
					PetsFuseUI.getInstance().putPetIn(((photoVG.getItemByIndex(_curSelect).getItem() as HeadItem).data as ViewObject));
				}
				else if(PetsUI.getInstance().checkUI(UnitUIDefine.PetsSwallowUI))
				{
					PetsSwallowUI.getInstance().putPetIn(((photoVG.getItemByIndex(_curSelect).getItem() as HeadItem).data as ViewObject));
				}*/
				return;
			}
			if(_curSelect!=-1)
			{
				//先清除原先选中的滤镜
				photoVG.getItemByIndex(_curSelect).filters	=	null;
			}
			var item	:HeadItem = photoVG.getItemByIndex(pos).getItem() as HeadItem
			//空对象返回
			if(null == item.data)
			{
				return;
			}
			_curSelect = pos;
			//添加滤镜
			photoVG.getItemByIndex(_curSelect).filters	=	[Filter.YELLOW_INNER];
			//更新属性面板数据
			_profileUI.data = item.data;
			_curObj = item.objId;
			PetsProfileUI.getInstance().selectPet(item.data as ViewObject);
			//技能切换
			/*NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHANGE_OBJ,_curObj);
			//装备切换
			if(_curMc.data != _player.data)
			{
				NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHANGE_EQUIP_OBJ,_curObj);
			}*/
		}
		
		/**
		 * 添加宠物 
		 * @param key
		 * @param val
		 * @param data
		 * 
		 */
		private function onAddPet(key:String, val:ViewObject, data:Object):void
		{
			if(val.GetPropertiesEx("Photo")=="")
			{
				return;
			}
			var pos:int	=	_petsData.length;
			var item	:HeadItem = photoVG.getItemByIndex(pos).getItem() as HeadItem;
			item.source = ResConfig.pngPhotoPath + val.GetPropertiesEx("Photo") + ".png";
			item.objId = val.objId;
			item.data = val;
			_petsData.push(val);
			/*if(null != photoVG.getItemByIndex(pos).getItem())
			{
				var item	:HeadItem = photoVG.getItemByIndex(pos).getItem() as HeadItem;
				item.source = ResConfig.pngPhotoPath + val.GetPropertiesEx("Photo") + ".png";
				item.objId = val.objId;
				item.data = val;
				var id:int	=	val.GetPropertiesEx("Pos") - 1;
				_petsData[id] = val;
			}*/
		}
		
		/**
		 * 移除宠物 
		 * @param key
		 * @param val
		 * @param data
		 * 
		 */
		private function onRemovePet(key:String, val:ViewObject, data:Object):void
		{
			//先清除头像列表
			clearList();
			//从数组删除内容
			var id	:int = _petsData.indexOf(val,0);
			_petsData.splice(id, 1);
			//重新显示头像列表
			freshList();
		}
		
		/**
		 * 刷新头像列表
		 * 
		 */
		private function freshList():void
		{
			for(var i:int=0;i<_petsData.length;i++)
			{
				var item	:HeadItem = photoVG.getItemByIndex(i).getItem() as HeadItem;
				var val		:ViewObject = _petsData[i];
				item.source = ResConfig.pngPhotoPath + val.GetPropertiesEx("Photo") + ".png";
				item.objId = val.objId;
				item.data = val;
			}
		}
		
		/**
		 * 清除头像列表，准备刷新 
		 * 
		 */
		private function clearList():void
		{
			var item	:HeadItem;
			for(var i:int=0;i<_petsData.length;i++)
			{
				item = photoVG.getItemByIndex(i).getItem() as HeadItem;
				item.objId = null;
				item.data = null;
				item.clearPhoto();
			}
		}
		
		/**
		 * 获取当前选择对象ID
		 * 
		 */
		public function get curObj():Point
		{
			return _curObj;
		}
		
		/**
		 * 重置，默认人
		 * 
		 */
		override public function reset():void
		{
			if(_curSelect!=-1)
			{
				//先清除原先选中的滤镜
				photoVG.getItemByIndex(_curSelect).filters	=	null;
			}
			_curSelect = -1
			onSelect(0);
		}
	}
}